import { Upload, message, Button } from 'antd';
import { UploadOutlined } from '@ant-design/icons';
import SecurityService from '@/pages/login/service';
import { serverUrlRoot } from '@/http';
const props = {
  name: 'file',
  action: serverUrlRoot + '/api/student/changeFace',
  headers: {
    Authorization: SecurityService.getSecurity(),
  },
  onChange(info: any) {
    if (info.file.status !== 'uploading') {
      console.log(info.file, info.fileList);
    }
    if (info.file.status === 'done') {
      message.success(`${info.file.name} 上传成功`).then();
      location.reload();
    } else if (info.file.status === 'error') {
      message.error(`${info.file.name} 上传失败`).then();
    }
  },
};

function beforeUpload(file: any) {
  const isJpgOrPng =
    file.type === 'image/jpeg' ||
    file.type === 'image/png' ||
    file.type === 'image/bmp' ||
    file.type === 'image/gif';
  if (!isJpgOrPng) {
    message.error('你只能上传 JPG/PNG/GIF/BMP 文件!').then();
  }
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    message.error('图片不能超过 2MB!').then();
  }
  return isJpgOrPng && isLt2M;
}

export default () => {
  return (
    <Upload {...props} beforeUpload={beforeUpload}>
      <Button
        style={{
          borderRadius: '10px',
          height: '40px',
        }}
        icon={<UploadOutlined />}
      >
        上传头像
      </Button>
    </Upload>
  );
};
